﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ClientAppViaWebService.LocationsLogicServiceRef;

namespace ClientAppViaWebService.Forms
{
    public partial class LocationEditForm : TObjectEditForm
    {
        private LocationsLogicService lService;
        private readonly Int32 ClassID = 28;
        private String respPersonId;

        #region Properties
        public float Square
        {
            get
            {

                return float.Parse(txtSquare.Text);
            }
            set { txtSquare.Text = value.ToString(); }
        }
        public String Responsible_Person
        {
            get
            {
                return txtResponsiblePerson.Text;
            }
            set { txtResponsiblePerson.Text = value; }
        }
        public String ResponsiblePersonId
        {
            get { return respPersonId; }
            set { respPersonId = value; }
        }
        #endregion
        
        public LocationEditForm()
        {
            InitializeComponent();
            lService = new LocationsLogicService();
            classTextBox.Text = ClassID.ToString();
        }         

        protected override void SetFormProperties(Object _object)
        {
            Location o = (_object as Location);
            ID = o.ID;
            EXT_ID = o.EXT_ID;
            ParentId = o.PARENT_ID;
            Class = o.CLASS_ID;
            ObjName = o.NAME;
            Square = o.SQUARE;
            ResponsiblePersonId = o.RESPONSIBLE_PERSON_ID;
            // TODO: set ResponsiblePerson
        }

        protected override Object RetrieveFormProperties()
        {
            Location obj = new Location();
            obj.ID = ID;
            obj.EXT_ID = EXT_ID;
            obj.PARENT_ID = ParentId;
            obj.CLASS_ID = Class;
            obj.NAME = ObjName;
            obj.DataBaseName = DataBaseName;
            obj.RESPONSIBLE_PERSON_ID = respPersonId;
            obj.SQUARE = Square;
            return obj;
        }

        protected override void LoadEditFormData(int parentNodeID)
        {
            Location p = lService.SelectLocation_By_DataBase_And_Id(DataBaseName, parentNodeID);
            SetFormProperties(p);
        }

        protected override void UpdateEntity()
        {
            Location obj = RetrieveFormProperties() as Location;
            lService.UpdateLocation(obj);
        }

        protected override void InsertEntity()
        {
            Location obj = RetrieveFormProperties() as Location;
            lService.InsertLocation(obj);
        }
    }
}
